<?php
namespace app\mp\admin;
use app\system\admin\Admin;
use app\common\model\Memo as MemoModel;

class Memo extends Admin
{
    protected $hisiModel = 'Memo';//模型名称[通用添加、修改专用]
    protected $hisiValidate = 'Memo';
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function initialize() {
    	parent::initialize();
    }

    public function index()
    {
    	if ($this->request->isAjax()) {
    		$where[]    = ['mm.status', '>', -1];
    		$page       = $this->request->param('page/d', 1);
    		$limit      = $this->request->param('limit/d', 20);
    		$clientOrdernum    = $this->request->param('client_ordernum/s');
    		$keyword    = $this->request->param('keyword/s');

            //订单加一级
            order_level($where,'o.');
            if (substr($clientOrdernum, 0, 1) == 'B') {
                $where[] = ['mm.order_material_id', '=', substr($clientOrdernum, 1, 10) ];
            }else{
                if ($clientOrdernum != '') {
                    $where[] = ['mm.client_ordernum', '=', $clientOrdernum];
                }
            }
            switch (env('env_site')) {
                case 'riwei':
                    $ext_id = trim($this->request->param('ext_id'));
                    if ($ext_id != '') {
                        $temp = db('order')->field('id')->where([['ext_id', '=', $ext_id]])->find();
                        $where[] = ['mm.order_id', '=', $temp['id']];
                    }
                    break;
            }
    		if ($keyword != '') {
    			$where[] = ['mm.content', 'like', "%{$keyword}%"];
    		}

            $order = 'mm.create_time desc,mm.status ASC';
            $data['data'] = db('memo mm')
                ->field('mm.*')
                ->join('order o', "mm.order_id=o.id", 'left')
                ->where($where)->page($page)->limit($limit)->order($order)->select();
            $data['count'] = db('memo mm')
                ->join('order o', "mm.order_id=o.id", 'left')
                ->where($where)->count();
    		$data['code'] = 0;
    		$data['msg'] = '';
    		return json($data);
    	}

    	$assign = [];
    	return $this->assign($assign)->fetch();
    }

}